import {
  Router
} from "../../utils/common.js";
const db = wx.cloud.database();
Page({
  data: {
    petRunList: [],
    petFriendsList: [],
    petFriendsId: [],
    arr: [],
    cardCur: 0,
    page: 1,
    totalCount: 0, //数据总数
    myDate: "",
    loadProgress: 0
  },

  onLoad() {
    let date = new Date();
    let myDate = date.toLocaleString(); //获取当前日期
    this.setData({
      myDate: myDate.substr(0, 10)
    });
    this.getPetRunList();
    this.getTotalCount();
    this.getPetFriendsList();
  },
  /**
   * 获取狗圈列表
   */
  getPetFriendsList() {
    let that = this;
    that.loadProgress();
    db.collection('p_pet_collect').where({
      openId: wx.getStorageSync('openid')
    }).get({
      success: res => {
        that.setData({
          petFriendsId: res.data
        });
        for (let i = 0; i < res.data.length; i++) {
          that.loadPetDet(res.data[i].petId, i);
        };
      },
      fail: err => {
        console.log('[数据库] [查询记录] 失败：');
      }
    })
  },
  /**
   * 获取宠物信息
   */
  loadPetDet(petId, i) {
    let that = this;
    let arr = that.data.arr;
    db.collection('p_pets').where({
      _id: petId
    }).get({
      success: res => {
        let info = res.data[0];
        arr.push(info);
        that.setData({
          arr: arr
        });
        // 判断是否是最后一次循环
        setTimeout(function () { that.isFinal(i); }, 500);

      },
      fail: err => {
        console.log('[数据库] [查询记录] 失败：');
      }
    })
  },
  isFinal(i) {
    let arr = this.data.petFriendsId;
    if (i == arr.length - 1) {
      this.setData({
        petFriendsList: this.data.arr,
        loadProgress: 100
      })
    }
  },
  /**
 * 获取总数
 */
  getTotalCount() {
    let that = this;
    db.collection('p_pet_run').where({
      updateTime: this.data.myDate
    }).count({
      success: function (res) {
        that.setData({
          totalCount: res.total
        })
      }
    })
  },
  /**
 * 今日遛狗人数
 */
  getPetRunList() {
    db.collection('p_pet_run').where({
      updateTime: this.data.myDate
    }).skip(0)
      .limit(10)
      .get({
        success: res => {
          this.setData({
            petRunList: res.data
          })
        },
        fail: err => {
          console.log('[数据库] [查询记录] 失败：');
        }
      })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page;
    if (this.data.petRunList.length < this.data.totalCount) {
      db.collection('p_pet_run').where({
        updateTime: this.data.myDate
      }).skip(page * 10)
        .limit(10)
        .get({
          success: res => {
            let petRunList = this.data.petRunList;
            for (let i = 0; i < res.data.length; i++) {
              petRunList.push(res.data[i]);
            }
            this.setData({
              petRunList: petRunList,
              page: page + 1
            })
          },
          fail: err => {
            console.log('[数据库] [查询记录] 失败：');
          }
        })

    } else {
      // wx.showToast({
      //   title: '没有更多数据了',
      // })
    }

  },
  navToPetDet: function (e) {
    let params = {
      petId: e.currentTarget.dataset.petid
    }
    Router.navigateTo("../dogdet/dogdet", params);
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  loadProgress() {
    this.setData({
      loadProgress: this.data.loadProgress + 3
    })
    if (this.data.loadProgress < 100) {
      setTimeout(() => {
        this.loadProgress();
      }, 100)
    } else {
      this.setData({
        loadProgress: 0
      })
    }
  }


})